package com.zwh.controller;


import com.zwh.entity.Employee;
import com.zwh.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private EmployeeService employeeService;

    //把图片文件目录注入进来
    @Value("${photo.file.dir}")
    private String realpath;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    //删除员工
    @RequestMapping("delete")
    public String delete(Integer id){//这里也是接受一个Integer类型的id
        //在删除数据前先保存一份，获取id
        String photo = employeeService.findById(id).getPhoto();
        //删除头像
        File file = new File(realpath, photo);
        if (file.exists()) file.delete();//删除IDEA里面的图片
        //删除数据
        employeeService.delete(id);

        return "redirect:/employee/lists";//删除成功后还是跳转到我们的员工列表
    }

    @RequestMapping("update")
    public String update(Employee employee,MultipartFile img) throws IOException {

        //这里是判断是否更新头像
        boolean notEmpty = !img.isEmpty();//这里是员工更新了头像
        if (notEmpty){
            //先删除老的员工头像，根据id查询原始头像
            String oldphoto = employeeService.findById(employee.getId()).getPhoto();
            File file = new File(realpath, oldphoto);
            if (file.exists())file.delete();//删除老的头像
            //上传新的头像
            String originalFilename = img.getOriginalFilename();
            String fileNamePrefix = new SimpleDateFormat("yyyyMMddmmssSSSS").format(new Date());
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = fileNamePrefix + fileNameSuffix;
            img.transferTo(new File(realpath,newFileName));
            //修改员工新的头像名称
            employee.setPhoto(newFileName);
        }

        //没有更新头像的话,我们就直接更新他的基本信息
        employeeService.update(employee);

        return "redirect:/employee/lists";//更新呈贡跳转到员工查询列表
    }

    //更新员工信息
    @RequestMapping("detail")
    public String detail(Integer id, Model model){//接收id,即查询员工的id
        //然后更具id去查询员工
        Employee employee = employeeService.findById(id);
        model.addAttribute("employee", employee);

        return "updateEmp";//跳转到我们的更新页面
    }

    //保存员工信息
    @RequestMapping("save")
    public String save(Employee employee, MultipartFile img) throws IOException {

        //处理头像上传，修改图片名称，！！！注意这里表单提交方式必须是post,2.还有表单的属性
        String fileNamePrefix = new SimpleDateFormat("yyyyMMddmmssSSSS").format(new Date());
        String originalFilename = img.getOriginalFilename();
        String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = fileNamePrefix + fileNameSuffix;
        img.transferTo(new File(realpath,newFileName));

        //保存员工的图片信息
        employee.setPhoto(newFileName);//保存头像名字
        employeeService.save(employee);

        return "redirect:/employee/lists";//保存成功就跳转到员工查询列表
    }

    @RequestMapping("/lists")
    public String lists(Model model){

        List<Employee> employeeList = employeeService.lists();
        model.addAttribute("employeeList",employeeList);

        return "emplist";
    }

}
